"""
TODO: 工具类文件夹
"""
# TODO：不同模块具备相同工具名需使用别名加以区分

from selenium import webdriver as web
from appium import webdriver as app

from config import WEB_URL


class DriverUtil:
    """
    浏览器对象和APP驱动对象类
    """
    __web_driver = None
    __app_driver = None
    __web_driver_switch = True

    @classmethod
    def get_web_driver(cls):
        """
        创建浏览器对象
        """
        # 判断浏览器对象不存在时创建
        if cls.__web_driver is None:
            cls.__web_driver = web.Chrome()
            cls.__web_driver.get(WEB_URL)
            # 最大化窗口
            cls.__web_driver.maximize_window()
            # 隐时等待
            cls.__web_driver.implicitly_wait(10)
        return cls.__web_driver

    @classmethod
    def quit_web_driver(cls):
        """
        浏览器退出方法
        """
        # TODO:判断浏览器对象存在在执行退出
        if cls.__web_driver and cls.__web_driver_switch:
            cls.__web_driver.quit()
            cls.__web_driver = None

    @classmethod
    def switch_driver_quit_method(cls, switch):
        """
        切换驱动对象开关方法
        :param switch: True 为开  ：False 为关
        """
        cls.__web_driver_switch = switch

    @classmethod
    def get_app_driver(cls):
        """
        驱动对象获取方法
        :return: 驱动对象
        """
        # TODO：判断驱动对象不存在时创建
        if cls.__app_driver is None:
            # 实列化驱动对象
            capabilities = dict()
            capabilities['platformName'] = 'Android'  # 设备系统平台：Android 和iOS
            capabilities['platformVersion'] = '7.1.2'  # 设备系统版本
            capabilities['deviceName'] = '模拟机'  # 设备名称
            capabilities['appPackage'] = 'com.tpshop.malls'  # [重要]待测App应用的包名
            capabilities['appActivity'] = '.SPMainActivity'  # [重要]待测App应用的界面名称

            # 设置；com.tpshop.malls/.SPMainActivity com.tpshop.malls/.SPMainActivity
            cls.__app_driver = app.Remote('http://127.0.0.1:4723/wd/hub', desired_capabilities=capabilities)
        return cls.__app_driver

    @classmethod
    def quit_app_driver(cls):
        """
        退出驱动对象
        """
        # TODO：判断驱动对象存在时退出
        if cls.__app_driver:
            cls.__app_driver.quit()
            cls.__app_driver = None


if __name__ == '__main__':
    DriverUtil.get_web_driver()
    DriverUtil.quit_web_driver()


